<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */

namespace app\command\kuaishou\timeframe;

use app\command\BaseWork;
use app\enum\ConfigEnum;
use app\enum\KuaiShouEnum;
use app\model\mysql\KsTimeFrameTaskContentModel;
use app\service\batch\kuaishou\UnitTimeFrameService;
use app\service\batch\KuaiShouBatchService;
use think\console\Input;
use think\console\Output;
use think\facade\Cache;

class UnitTimeFrameContentPushWork extends BaseWork
{
    protected function configure()
    {
        // 指令配置
        $this->setName('ks_unit_time_frame_content_push_work');
        $this->setDescription('快手批量修改组投放时段推送');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        ini_set('memory_limit', '-1');
        $this->getOutputMessage('快手批量修改组投放时段推送开始');
        $st = time() + 300;
        $redis = Cache::store('redis')->handler();
        $key = ConfigEnum::CACHE_CONFIG[KuaiShouEnum::BATCH_CONFIG_KEY]['unit_frame_time_push'];
        while (time() < $st) {
            try {
                $contentId = $redis->lPop($key);
                if (! $contentId) {
                    usleep(500000);
                    continue;
                }
                $this->getOutputMessage('快手批量修改组投放时段推送[' . $contentId . ']开始');
                if (KuaiShouBatchService::batchUintFrameTimeUpdate(intval($contentId))) {
                    $this->getOutputMessage('快手批量修改组投放时段推送[' . $contentId . ']成功');
                } else {
                    $this->getOutputMessage('快手批量修改组投放时段推送[' . $contentId . ']失败,' . KuaiShouBatchService::getError(0));
                }
            } catch (\Exception $e) {
                return self::setError($e->getMessage());
            }
        }
        $this->getOutputMessage('快手批量修改组投放时段推送结束');
    }
}
